/**
 * Alibaba.com.cn Inc.
 * Copyright (c) 2004-2021 All Rights Reserved.
 */
package com.smart.context;

import cn.dev33.satoken.stp.StpUtil;
import com.smart.model.vo.JiansheUnitVO;
import com.smart.model.vo.OperateUserVO;
import com.smart.model.vo.ProjectVO;
import com.smart.model.vo.SuperviseUserVO;

import java.util.Optional;

/**
 * @author zhanghong
 * @version SessionContext.java, v 0.1 2023/6/21 17:54 下午 SessionContext Exp $$
 */
public class SessionContext {
    public static ThreadLocal<BaseSession> context = new ThreadLocal<>();
    /**
     * 获取当前登陆用户信息
     * @return
     */
    public static Long getLoginAccountId() {
        BaseSession baseSession = context.get();
        if (baseSession == null) {
            return null;
        }
        if (baseSession.getOperateUser() != null) {
            //判断是否登录
            //StpUtil.checkLogin();
            if (StpUtil.isLogin()) {
                return baseSession.getOperateUser().getId();
            }
        }
        if (baseSession.getProject() != null) {
            return baseSession.getProject().getId();
        }
        if (baseSession.getJiansheUnit() != null) {
            return baseSession.getJiansheUnit().getId();
        }
        if (baseSession.getSuperviseUser() != null) {
            return baseSession.getSuperviseUser().getId();
        }
        return null;
    }
    /**
     * 云监管平台账号id
     * @return
     */
    public static Long getSuperviseUserId() {
        BaseSession baseSession = context.get();
        return Optional.ofNullable(baseSession).map(BaseSession::getSuperviseUser).map(SuperviseUserVO::getId).orElse(null);
    }

    /**
     * 是否是监管账户
     * @return
     */
    public static Boolean isJianGuanAccount() {
        return getSuperviseUserId() != null;
    }

    /**
     * 云监管平台对象
     * @return
     */
    public static SuperviseUserVO getSuperviseVo() {
        BaseSession baseSession = context.get();
        return Optional.ofNullable(baseSession).map(BaseSession::getSuperviseUser).orElse(null);
    }
    /**
     * 云服务平台账号id
     * @return
     */
    public static Long getProjectId() {
        BaseSession baseSession = context.get();
        return Optional.ofNullable(baseSession).map(BaseSession::getProject).map(ProjectVO::getId).orElse(null);
    }
    /**
     * 云管理平台账号id
     * @return
     */
    public static Long getJiansheUnitId() {
        BaseSession baseSession = context.get();
        return Optional.ofNullable(baseSession).map(BaseSession::getJiansheUnit).map(JiansheUnitVO::getId).orElse(null);
    }
    /**
     * 运营管理平台账号id
     * @return
     */
    public static Long getOperateUserId() {
        BaseSession baseSession = context.get();
        return Optional.ofNullable(baseSession).map(BaseSession::getOperateUser).map(OperateUserVO::getId).orElse(null);
    }

    public static String getIp() {
        return null;
    }
}
